/*
 * The MIT License
 * 
 * Copyright (c) 2011 Takahiro Hashimoto
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.xiyuan.smartutils.client.ws.core.handler.impl;

import com.xiyuan.smartutils.client.ws.client.WebSocketClient;
import com.xiyuan.smartutils.client.ws.core.handler.StreamHandler;
import com.xiyuan.smartutils.client.exception.WebSocketException;
import com.xiyuan.smartutils.client.ws.util.PacketDumpUtil;

import java.nio.ByteBuffer;

import static com.xiyuan.smartutils.client.ws.util.PacketDumpUtil.*;

/**
 * Dump mode: HS_UP : dump buffer of handshake upstream HS_DOWN: dump buffer of
 * handshake downstream FR_UP : dump buffer of frame upstream FR_DOWN: dump
 * buffer of frame downstream ALL : dump these streams
 *
 * @author xiyuan 2022-46-09 @version 1.0.0 新建或整理
 *
 */
public class PacketDumpStreamHandler implements StreamHandler
{

    /** The log. */
    // private Logger log =
    // Logger.getLogger(PacketDumpStreamHandler.class.getName());

    /**
     * Instantiates a new packet dump stream handler.
     */
    public PacketDumpStreamHandler()
    {
    }

    @Override
    public void writeHandshake(WebSocketClient ws, ByteBuffer buffer) throws WebSocketException
    {
        this.packetDump(ws, HS_UP, "HS Upstream", buffer);
    }

    @Override
    public void readHandshake(WebSocketClient ws, ByteBuffer buffer) throws WebSocketException
    {
        this.packetDump(ws, HS_DOWN, "HS Downstream", buffer);
    }

    @Override
    public void write(WebSocketClient ws, ByteBuffer buffer) throws WebSocketException
    {
        this.packetDump(ws, FR_UP, "FR Upstream", buffer);
    }

    @Override
    public void read(WebSocketClient ws, ByteBuffer buffer) throws WebSocketException
    {
        this.packetDump(ws, FR_DOWN, "FR Downstream", buffer);
    }

    private void packetDump(WebSocketClient ws, int dumpModel, String title, ByteBuffer buffer) throws WebSocketException
    {
        int p = buffer.position(), l = buffer.limit();
        if (PacketDumpUtil.isDump(ws, dumpModel))
        {
            PacketDumpUtil.printPacketDump(title, buffer);
            // reverse buffer
            buffer.position(p);
            buffer.limit(l);
        }
    }
}
